/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009  Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.edu.ufcg.ccc.javalog.view;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import br.edu.ufcg.ccc.javalog.model.Franchise;
import br.edu.ufcg.ccc.javalog.model.Package;
import br.edu.ufcg.ccc.javalog.model.Validator;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * View for package list.
 * 
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 08/11/09
 */
@SuppressWarnings("serial") // not serializing this...
public class PackageListView extends AbstractView {
	private JLabel labelTitle;
	private AbstractAction actionSeePackage;
	private JButton buttonSeePackage;
	private JList listPackages;
	private AbstractAction actionBack;
	private JButton buttonBack;
	private JScrollPane scrollListPackages;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PackageListView inst = new PackageListView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	private PackageListView() {
		super();
		initGUI();
	}
	
	/**
	 * Creates this view for the specified franchise model object.
	 * @param franchise the model to view
	 */
	public PackageListView(Franchise f) {
		this();
		Validator.validateObject(f, new IllegalArgumentException("null franchise received"));
		refreshView();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Lista de Pacotes");
			{
				labelTitle = new JLabel();
				labelTitle.setText("Pacotes cadastrados no sistema");
			}
			{
				scrollListPackages = new JScrollPane();
				scrollListPackages.setSize(30000, 30000);
				scrollListPackages.setViewportView(getListPackages());
			}
			{
				buttonBack = new JButton();
				buttonBack.setText("Voltar");
				buttonBack.setAction(getActionBack());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(labelTitle, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(scrollListPackages, 0, 370, Short.MAX_VALUE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(buttonBack, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(getButtonSeePackage(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(labelTitle, GroupLayout.Alignment.LEADING, 0, 608, Short.MAX_VALUE)
				    .addComponent(scrollListPackages, GroupLayout.Alignment.LEADING, 0, 608, Short.MAX_VALUE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(0, 371, Short.MAX_VALUE)
				        .addComponent(getButtonSeePackage(), GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				        .addComponent(buttonBack, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap());
			pack();
			this.setSize(640, 480);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	* This method should return an instance of this class which does 
	* NOT initialize it's GUI elements. This method is ONLY required by
	* Jigloo if the superclass of this class is abstract or non-public. It 
	* is not needed in any other situation.
	 */
	public static Object getGUIBuilderInstance() {
		return new PackageListView(Boolean.FALSE);
	}
	
	/**
	 * This constructor is used by the getGUIBuilderInstance method to
	 * provide an instance of this class which has not had it's GUI elements
	 * initialized (ie, initGUI is not called in this constructor).
	 */
	private PackageListView(Boolean initGUI) {
		super();
	}
	
	private AbstractAction getActionBack() {
		if(actionBack == null) {
			actionBack = new AbstractAction("Voltar", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return actionBack;
	}
	
	private JList getListPackages() {
		if(listPackages == null) {
			ListModel listPackagesModel = 
				new DefaultComboBoxModel(
						new String[] { "Não há nenhum pacote no sistema." });
			listPackages = new JList();
			listPackages.setModel(listPackagesModel);
			listPackages.setPreferredSize(new java.awt.Dimension(1200, 30000));
			listPackages.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listPackages.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					listPackagesMouseClicked(evt);
				}
			});
		}
		return listPackages;
	}

	private void refreshView() {
		Map<String, Package> packages = Franchise.getPackages();
		if(!packages.isEmpty()) {
			listPackages.setEnabled(true);
			listPackages.setListData(packages.values().toArray());
		}
		else {
			listPackages.setListData(new String[] {"Não há nenhum pacote no sistema."});
			listPackages.setEnabled(false);
		}
	}
	
	private JButton getButtonSeePackage() {
		if(buttonSeePackage == null) {
			buttonSeePackage = new JButton();
			buttonSeePackage.setText("Ver Pacote");
			buttonSeePackage.setAction(getActionSeePackage());
		}
		return buttonSeePackage;
	}
	
	private AbstractAction getActionSeePackage() {
		if(actionSeePackage == null) {
			actionSeePackage = new AbstractAction("Ver Pacote", null) {
				public void actionPerformed(ActionEvent evt) {
					if(!listPackages.isEnabled() || listPackages.getSelectedValue() == null) {
						JOptionPane.showMessageDialog(PackageListView.this, "Nenhum pacote foi selecionado.", "Erro", JOptionPane.ERROR_MESSAGE);
					}
					else {
						Package selectedPackage = (Package)listPackages.getSelectedValue();
						WindowManager.getInstance().openNewWindow(new PackageView(selectedPackage));
					}
				}
			};
		}
		return actionSeePackage;
	}
	
	/**
	 * Emulate double click.
	 * @param evt the mouse event
	 */
	private void listPackagesMouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2) {
            buttonSeePackage.doClick(); 
        }
	}
}
